# Bubble Sort 冒泡排序
import numpy as np
import pandas as pd
import plotly.graph_objs as go
import plotly.express as px

lst = np.random.randint(-100, 500, size=20)


def express_barchart(df: pd.DataFrame):
    width = 720
    height = 720
    title = "冒泡排序"
    diagram = px.bar(data_frame=df, x=df.index, y="value", color=df.index, width=width, height=height, title=title,
                     animation_frame="count", template="plotly_dark")
    diagram.show()


def bubblesort2df(lst):
    """
     @Description: 冒泡排序
     : @params:  lst: list of number
     : @return:  dataframe of evertime bubblesort list
     :
     : @Creator: chenyj
     : @Date: 2023/10/22 14:39
     :
     : @Modify:
     : @Date:
     :
    """
    cnt = 1
    bubbleSort = pd.DataFrame({"value": lst})
    bubbleSort["count"] = 1

    for i in range(len(lst) - 1):
        for j in range(len(lst) - 1 - i):
            if lst[j] > lst[j + 1]:
                lst[j], lst[j + 1] = lst[j + 1], lst[j]
            cnt += 1
            tempLstPD = pd.DataFrame({
                "value": lst,
                "count": cnt
            })
            bubbleSort = pd.concat([bubbleSort, tempLstPD], ignore_index=False)
            # bubbleSort.loc[len(bubbleSort)] = lst
    return bubbleSort


if __name__ == '__main__':
    print(lst)
    dfBubbleSort = bubblesort2df(lst)
    express_barchart(dfBubbleSort)
